<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="LeetCode,">










<meta name="description" content="原始二分查找 Input : [1,2,3,4,5]key : 3return the index : 2  12345678910const binarySearch = (nums, key) =&amp;gt; &amp;#123;  let l = 0, h = nums.length - 1, mid  while (l &amp;lt;= h) &amp;#123;    mid = Math.floor(l + (">
<meta name="keywords" content="LeetCode">
<meta property="og:type" content="article">
<meta property="og:title" content="二分查找">
<meta property="og:url" content="https://xiver.gitee.io/2019/06/30/二分查找/index.html">
<meta property="og:site_name" content="Condi">
<meta property="og:description" content="原始二分查找 Input : [1,2,3,4,5]key : 3return the index : 2  12345678910const binarySearch = (nums, key) =&amp;gt; &amp;#123;  let l = 0, h = nums.length - 1, mid  while (l &amp;lt;= h) &amp;#123;    mid = Math.floor(l + (">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-06-30T14:39:51.248Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="二分查找">
<meta name="twitter:description" content="原始二分查找 Input : [1,2,3,4,5]key : 3return the index : 2  12345678910const binarySearch = (nums, key) =&amp;gt; &amp;#123;  let l = 0, h = nums.length - 1, mid  while (l &amp;lt;= h) &amp;#123;    mid = Math.floor(l + (">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://xiver.gitee.io/2019/06/30/二分查找/">





  <title>二分查找 | Condi</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Condi</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://xiver.gitee.io/2019/06/30/二分查找/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Condi">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Condi">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">二分查找</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-30T21:08:46+08:00">
                2019-06-30
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="原始二分查找"><a href="#原始二分查找" class="headerlink" title="原始二分查找"></a>原始二分查找</h1><blockquote>
<p>Input : [1,2,3,4,5]<br>key : 3<br>return the index : 2</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> binarySearch = <span class="function">(<span class="params">nums, key</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> l = <span class="number">0</span>, h = nums.length - <span class="number">1</span>, mid</span><br><span class="line">  <span class="keyword">while</span> (l &lt;= h) &#123;</span><br><span class="line">    mid = <span class="built_in">Math</span>.floor(l + (h - l) / <span class="number">2</span>)</span><br><span class="line">    <span class="keyword">if</span> (nums[mid] === key) <span class="keyword">return</span> mid</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (nums[mid] &gt; key) h = mid - <span class="number">1</span></span><br><span class="line">    <span class="keyword">else</span> l = mid + <span class="number">1</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>时间复杂度：二分查找，每次将查找区间减半，这种折半特性的时间复杂度为 O(logN)</p>
<p>返回值：</p>
<ul>
<li>-1：表示没有查找到 key。</li>
<li>l：表示插入到 nums 中的正确位置。</li>
</ul>
<p>变种：</p>
<p>二分查找有很多变种，变种要注意边界值的判断。例如在一个有重复元素中的数组中查找最左边的 key 值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> binarySearch = <span class="function">(<span class="params">nums, key</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> l = <span class="number">0</span>, h = nums.length - <span class="number">1</span>, mid</span><br><span class="line">  <span class="keyword">while</span> (l &lt; h) &#123;</span><br><span class="line">    mid = <span class="built_in">Math</span>.floor(l + (h - l) / <span class="number">2</span>)</span><br><span class="line">    <span class="keyword">if</span> (key &lt;= nums[mid]) h = mid</span><br><span class="line">    <span class="keyword">else</span> l = mid + <span class="number">1</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> l</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>和上述的实现有什么不同呢？</p>
<ul>
<li>赋值表达式变成了 h = mid</li>
<li>循环语句变成了 l &lt; h</li>
<li>最后返回的是 l</li>
</ul>
<p>因为要找最左的值，所以设置 key &lt;= nums[mid] 时，最左的区间位于 [l,mid] 中，此时将 h = mid，因为 mid 所在的位置也可以是解。</p>
<p>如果要找最右的值，应当设置 key &gt;= nums[mid] 时，最右的区间位于 [mid,h] 中，此时将 l = mid，因为 mid 所在的位置也可以是解。</p>
<p>在 h = m 情况下，如果循环条件为 l &lt;= h，那么有可能会出现无限循环无法退出的情况。</p>
<h1 id="求开方"><a href="#求开方" class="headerlink" title="求开方"></a>求开方</h1><blockquote>
<p>LeetCode 69</p>
<p>Input: 4<br>Output: 2<br>Input: 8<br>Output: 2</p>
<p>如果开方开不尽，开方结果向下取整。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> mySqrt = <span class="function"><span class="keyword">function</span> (<span class="params">x</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> l = <span class="number">1</span>, h = x</span><br><span class="line">  <span class="keyword">while</span> (l &lt;= h) &#123;</span><br><span class="line">    <span class="keyword">let</span> mid = <span class="built_in">Math</span>.floor(l + (h - l) / <span class="number">2</span>)</span><br><span class="line">    <span class="keyword">let</span> sqrt = <span class="built_in">Math</span>.floor(x / mid)</span><br><span class="line">    <span class="keyword">if</span> (sqrt === mid) <span class="keyword">return</span> mid</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (sqrt &lt; mid) h = mid - <span class="number">1</span></span><br><span class="line">    <span class="keyword">else</span> l = mid + <span class="number">1</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> h</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>通过二分查找来查找开方数 sqrt，因为 sqrt * sqrt = x。所以可以对 1 -  x 的区间进行二分查找。</p>
<p>本题关键是最后的返回值，退出循环时， l &gt; h，当开方开不尽的时候，结果就是 h ，为什么是 h 不是 l 呢？循环退出时， h 总是比 l 小1，由于本题要向下取整，所以取 h。</p>
<h1 id="大于给定元素的最小元素"><a href="#大于给定元素的最小元素" class="headerlink" title="大于给定元素的最小元素"></a>大于给定元素的最小元素</h1><blockquote>
<p>LeetCode 744</p>
<p>Input:<br>letters = [“c”, “f”, “j”]<br>target = “d”<br>Output: “f”<br>Input:<br>letters = [“c”, “f”, “j”]<br>target = “k”<br>Output: “c”</p>
<p>题目描述：给定一个有序的字符数组 letters，再给定一个字符 target，要求在 letters 中查找大于 target 的最小字符。如果找不到就返回第一个元素。</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> nextGreatestLetter = <span class="function"><span class="keyword">function</span> (<span class="params">letters, target</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> l = <span class="number">0</span>, h = letters.length - <span class="number">1</span></span><br><span class="line">  <span class="keyword">let</span> mid</span><br><span class="line">  <span class="keyword">while</span> (l &lt;= h) &#123;</span><br><span class="line">    mid = <span class="built_in">Math</span>.floor(l + (h - l) / <span class="number">2</span>)</span><br><span class="line">    <span class="keyword">if</span> (letters[mid] &lt;= target) &#123;</span><br><span class="line">      l = mid + <span class="number">1</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      h = mid - <span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> l &lt; letters.length ? letters[l] : letters[<span class="number">0</span>]</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>由于字符数组是有序的，根据有序数组查找指定字符，想到用二分查找。</p>
<h1 id="有序数组的-Single-Element"><a href="#有序数组的-Single-Element" class="headerlink" title="有序数组的 Single Element"></a>有序数组的 Single Element</h1><blockquote>
<p>LeetCode 540</p>
<p>Input: [1, 1, 2, 3, 3, 4, 4, 8, 8]<br>Output: 2</p>
<p>一个有序数组只有一个数不出现两次，找出这个数</p>
</blockquote>
<p>时间复杂度要求 O(logN)，因此很容易想到用二分查找。</p>
<p>假设index 为 Single Element 出现在数组中的位置。在 index 之后，数组原来的下标状态将会改变。如果 m 为偶数， m+1 &lt; index，那么，nums[m] = nums[m+1]；m+1 &gt;=index，那么 nums[m] != nums[m+1]</p>
<p>所以可知， 如果 nums[m] === nums[m+1] 则 target 所在的区间为 [m+2,h]；如果 nums[m] !=== nums[m+1] 则 target 所在区间为 [l,m]</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> singleNonDuplicate = <span class="function"><span class="keyword">function</span> (<span class="params">nums</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> l = <span class="number">0</span>, h = nums.length - <span class="number">1</span>, mid</span><br><span class="line">  <span class="keyword">while</span> (l &lt; h) &#123;</span><br><span class="line">    mid = <span class="built_in">Math</span>.floor(l + (h - l) / <span class="number">2</span>)</span><br><span class="line">    <span class="keyword">if</span> (mid % <span class="number">2</span> === <span class="number">1</span>) mid -= <span class="number">1</span></span><br><span class="line">    <span class="keyword">if</span> (nums[mid] === nums[mid + <span class="number">1</span>]) l = mid + <span class="number">2</span></span><br><span class="line">    <span class="keyword">else</span> h = mid</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> nums[h]</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="第一个错误的版本"><a href="#第一个错误的版本" class="headerlink" title="第一个错误的版本"></a>第一个错误的版本</h1><blockquote>
<p>LeetCode 278</p>
<p>题目描述：找到一个错误版本。</p>
<p>如果第 mid 个版本出错，那么第一个错误版本所在区间为 [l,mid]，此时 h=mid；否认第一个错误区间为 [mid+1,h],此时 l=mid+1</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> solution = <span class="function"><span class="keyword">function</span> (<span class="params">isBadVersion</span>) </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @param &#123;integer&#125; n Total versions</span></span><br><span class="line"><span class="comment">     * @return &#123;integer&#125; The first bad version</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params">n</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">let</span> l = <span class="number">1</span>, h = n, mid</span><br><span class="line">        <span class="keyword">while</span> (l &lt; h) &#123;</span><br><span class="line">            mid = <span class="built_in">Math</span>.floor(l + (h - l) / <span class="number">2</span>)</span><br><span class="line">            <span class="keyword">if</span> (isBadVersion(mid)) h = mid</span><br><span class="line">            <span class="keyword">else</span> l = mid + <span class="number">1</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> l</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="旋转数组的最小数字"><a href="#旋转数组的最小数字" class="headerlink" title="旋转数组的最小数字"></a>旋转数组的最小数字</h1><blockquote>
<p>LeetCode 153</p>
<p>Input: [3,4,5,1,2],<br>Output: 1</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> findMin = <span class="function"><span class="keyword">function</span> (<span class="params">nums</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> l = <span class="number">0</span>, h = nums.length - <span class="number">1</span></span><br><span class="line">  <span class="keyword">let</span> mid</span><br><span class="line">  <span class="keyword">while</span> (l &lt; h) &#123;</span><br><span class="line">    mid = <span class="built_in">Math</span>.floor(l + (h - l) / <span class="number">2</span>)</span><br><span class="line">    <span class="keyword">if</span> (nums[mid] &gt;= nums[h]) l = mid + <span class="number">1</span></span><br><span class="line">    <span class="keyword">else</span> h = mid</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> nums[l]</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>如果第 mid 个数字比 nums[h]小，则最小数字在  [l,mid] 之中， h=mid，否则最小数字在 [mid+1,h]之中。</p>
<h1 id="查找区间"><a href="#查找区间" class="headerlink" title="查找区间"></a>查找区间</h1><blockquote>
<p>LeetCode 34</p>
<p>Input: nums = [5,7,7,8,8,10], target = 8<br>Output: [3,4]<br>Input: nums = [5,7,7,8,8,10], target = 6<br>Output: [-1,-1]</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> searchRange = <span class="function"><span class="keyword">function</span> (<span class="params">nums, target</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> first = binarySearch(nums, target)</span><br><span class="line">  <span class="keyword">const</span> last = binarySearch(nums, target + <span class="number">1</span>) - <span class="number">1</span></span><br><span class="line">  <span class="keyword">if</span> (nums[first] !== target) <span class="keyword">return</span> [<span class="number">-1</span>, <span class="number">-1</span>]</span><br><span class="line">  <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> [first, <span class="built_in">Math</span>.max(last, first)]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> binarySearch = <span class="function">(<span class="params">nums, target</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> l = <span class="number">0</span>, h = nums.length</span><br><span class="line">  <span class="keyword">let</span> mid</span><br><span class="line">  <span class="keyword">while</span> (l &lt; h) &#123;</span><br><span class="line">    mid = <span class="built_in">Math</span>.floor(l + (h - l) / <span class="number">2</span>)</span><br><span class="line">    <span class="keyword">if</span> (nums[mid] &gt;= target) h = mid</span><br><span class="line">    <span class="keyword">else</span> l = mid + <span class="number">1</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> l</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果要查找的值比当前值小或者相等，则要查找的值的最左边在 [l,mid] 此时 h = mid，否则，要查找的值在 [mid+1,h]。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/LeetCode/" rel="tag"># LeetCode</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/06/30/贪心思想/" rel="next" title="贪心思想">
                <i class="fa fa-chevron-left"></i> 贪心思想
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/07/16/test/" rel="prev" title="test">
                test <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Condi</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#原始二分查找"><span class="nav-number">1.</span> <span class="nav-text">原始二分查找</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#求开方"><span class="nav-number">2.</span> <span class="nav-text">求开方</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#大于给定元素的最小元素"><span class="nav-number">3.</span> <span class="nav-text">大于给定元素的最小元素</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#有序数组的-Single-Element"><span class="nav-number">4.</span> <span class="nav-text">有序数组的 Single Element</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#第一个错误的版本"><span class="nav-number">5.</span> <span class="nav-text">第一个错误的版本</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#旋转数组的最小数字"><span class="nav-number">6.</span> <span class="nav-text">旋转数组的最小数字</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#查找区间"><span class="nav-number">7.</span> <span class="nav-text">查找区间</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Condi</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
